package com.lagou.edu.service.impl;

import com.lagou.edu.dao.UserDao;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.CodeFeignService;
import com.lagou.edu.service.UserService;
import com.lagou.edu.vo.LoginReqVo;
import com.lagou.edu.vo.RegisterReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private CodeFeignService feignService;


    @Override
    public String register(RegisterReqVo registerReqVo) {
        //校验用户是否已注册
        User byEmail = userDao.findByEmail(registerReqVo.getEmail());
        if(null != byEmail){
            return "用户已注册,请直接登录";
        }
        //校验调用code服务校验code是否过期
        String str = feignService.validateCode(registerReqVo.getCode());
        if(str.equals("yes")){
            //注册
            User user = new User(); //User和生成的token公用一张表吧
            String email = registerReqVo.getEmail();
            String password  = registerReqVo.getPassword();
            user.setEmail(email);
            user.setPassword(password);
            user.setId(Long.parseLong(new Random().nextInt()+""));
            userDao.save(user);
        }
        return str;
    }

    @Override
    public String login(LoginReqVo loginReqVo) {
        User user = userDao.findByEmail(loginReqVo.getEmail());
        if(null == user){
            return "用户不存在，请先注册";
        }
        if(!user.getPassword().equals(loginReqVo.getPassword())){
            return "账号或密码不对";
        }
        return "yes";
    }


}
